<template>
  <el-form
    :model="powerForm"
    label-width="100px">
    <el-form-item label="权限名称" prop="name">
      <el-input v-model="powerForm.name"></el-input>
    </el-form-item>
    <el-form-item label="权限路径" prop="url">
      <el-input v-model="powerForm.url"></el-input>
    </el-form-item>
    <el-form-item label="权限类型" prop="type">
      <el-input v-model="powerForm.type"></el-input>
    </el-form-item>
    <el-form-item label="权限状态" prop="status">
      <el-input v-model="powerForm.status"></el-input>
    </el-form-item>
    <el-form-item label="父亲节点" prop="pid">
      <el-input v-model="powerForm.pid"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('powerForm')">立即创建</el-button>
      <el-button @click="resetForm('powerForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      powerForm: {
        id: "",
        name: "用户管理",
        url: "http://localhost:8989/rbac-system/user/insert",
        type: "1",
        status: "1",
        pid: "",
      },
    };
  }, 
  mounted:function(){
    var app=this;
    if (this.$route.params.id) {
       app.$httpRequest
        .get("power/getById/" + app.$route.params.id,{})
        .then(function ({data}) {
          console.log("查询id");
          console.log(data);
          app.powerForm=data.data;
        })
        .catch(function (error) {
          //失败后调用
        })
        .then(function () {
          //不论成功或者失败后都调用
        });
    }
  },
  methods: {
    submitForm(formName) {
      console.log(this.$route.params.id);
      if (this.$route.params.id) {
        this.powerForm.id = this.$route.params.id;
        var app = this;
        this.$httpRequest.post("power/update", this.powerForm)
          .then(function ({data}) {
            if (data.code == 200) {
              console.log("修改成功");
              app.$message({
                showClose: true,
                message: "恭喜你,修改成功",
                type: "success",
              });
              app.$router.push("/toPermission");
            }
          })
          .catch(function (error) {
            //失败后调用
          })
          .then(function () {
            //不论成功或者失败后都调用
          });
      } else {
        var app = this;
        this.$httpRequest.post("power/insert", this.powerForm)
          .then(function ({data}) {
            if (data.code == 200) {
              app.$message({
                showClose: true,
                message: "恭喜你，添加成功",
                type: "success",
              });
              console.log("添加成功");
              app.$router.push("/toPermission");
            }
          })
          .catch(function (error) {
            //失败后调用
          })
          .then(function () {
            //不论成功或者失败后都调用
          });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toIndex() {
      console.log("进来了");
      this.$router.push("/toPermission");
    },
  },
};
</script>

<style>
</style>